package com.chao.listeners;

import com.chao.utils.SendMailUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.chao.utils.RabbitMqUtils.CG_EMAIL_DIRECT;
import static com.chao.utils.RabbitMqUtils.DIRECT_QUEUE_ONE;

@Slf4j
@Component
public class MqListeners {

    // @RabbitListener(queues = "chao.queue")
    // public void listenerQueueMessage(String msg) {
    //     System.out.println("消费者收到消息 = " + msg);
    // }
    //
    // @RabbitListener(queues = "work.queue")
    // public void listenerWork1QueueMessage(String msg) {
    //     System.out.println("消费者2收到消息 = " + msg);
    // }
    //
    // @RabbitListener(queues = "work.queue")
    // public void listenerWork2QueueMessage(String msg) {
    //     System.err.println("消费者1收到消息 = " + msg);
    // }
    //
    // @RabbitListener(queues = "fanout.queue1")
    // public void listenerFanout1QueueMessage(String msg) {
    //     System.out.println("消费者fanout.queue1收到消息 = " + msg);
    // }
    //
    // @RabbitListener(queues = "fanout.queue2")
    // public void listenerFanout2QueueMessage(String msg) {
    //     System.err.println("消费者fanout.queue2收到消息 = " + msg);
    // }
    //
    // @RabbitListener(queues = "object.queue")
    // public void listenerObjectQueueMessage(String msg) {
    //     System.err.println("object.queue2收到消息 = " + msg);
    // }

    // @RabbitListener(queues = "direct.queue1")
    // public void listenerDirect1QueueMessage(String msg) {
    //     System.out.println("消费者direct.queue1收到消息 = " + msg);
    // }

    // @RabbitListener(queues = "direct.queue2")
    // public void listenerDirect2QueueMessage(String msg) {
    //     System.err.println("消费者direct.queue2收到消息 = " + msg);
    // }


    // /**
    //  * 通过注解嵌套的方式绑定交换机和队列
    //  * @param msg
    //  */
    // @RabbitListener(bindings = @QueueBinding(
    //         value = @Queue(name = DIRECT_QUEUE1,declare = "true"),
    //         exchange = @Exchange(name = DIRECT_EXCHANGES,type = ExchangeTypes.DIRECT),
    //         key = {"red","blue"}
    // ))
    // public void listenerBindDirect1QueueMessage(String msg) {
    //     System.out.println("消费者BindDirect1queue收到消息 = " + msg);
    // }
    //
    // /**
    //  * 通过注解嵌套的方式绑定交换机和队列
    //  * @param msg
    //  */
    // @RabbitListener(bindings = @QueueBinding(
    //         value = @Queue(name = DIRECT_QUEUE2,declare = "true"),
    //         exchange = @Exchange(name = DIRECT_EXCHANGES,type = ExchangeTypes.DIRECT),
    //         key = {"red","yellow"}
    // ))
    // public void listenerBindDirect2QueueMessage(String msg) {
    //     System.out.println("消费者BindDirect1queue收到消息 = " + msg);
    // }


    @RabbitListener(queues = DIRECT_QUEUE_ONE)
    public void listenerDirectQueueMessage(String msg) {
        System.out.println("消费者DIRECT_QUEUE_ONE收到消息 = " + msg);
        throw new RuntimeException("故意的");
    }

    @Autowired
    public SendMailUtils sendMailUtils;


    @RabbitListener(queues = CG_EMAIL_DIRECT)
    public void listenerDirect2QueueMessage(Map<String, String> map) {
        if (map != null) {
            sendMailUtils.sendEmail(map.get("subject"), map.get("receiver"), map.get("text")
            );
        }
    }

    /**
     * 通过Jackson2JsonMessageConverter 发送对象消息实现转换
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
